﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ControlDefinitionUtil.h"
#include "LabelDefinition.h"
#include "LabelDefinitionReference.h"
#include "GalleryDefinition.h"
#include "GalleryDefinitionReference.h"
#include "LineEditDefinition.h"
#include "LineEditDefinitionReference.h"
#include "CheckBoxGroupDefinition.h"
#include "CheckBoxGroupDefinitionReference.h"
#include "RadioButtonGroupDefinition.h"
#include "RadioButtonGroupDefinitionReference.h"
#include "IControlDefinitionChangeEvent.h"
#include "ComboBoxDefinition.h"
#include "ComboBoxDefinitionReference.h"
#include "ProgressBarDefinition.h"
#include "ProgressBarDefinitionReference.h"
#include "CommandButtonDefinition.h"
#include "CommandButtonDefinitionReference.h"
#include "CommandButtonMenuDefinition.h"
#include "CommandButtonMenuDefinitionReference.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

// ControlDefinitionUtil 类实现
namespace gcmp
{
gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const ILineEditDefinition* pLineEditDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLineEditDefinition, L"pLineEditDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const LineEditDefinition* pLineEditDefine = dynamic_cast<const LineEditDefinition*>(pLineEditDefinition))
    {
        return pLineEditDefine->GetControlDefinitionChangeEvent();
    }
    else if (const LineEditDefinitionReference* pLineEditDefineReference = dynamic_cast<const LineEditDefinitionReference*>(pLineEditDefinition))
    {
        return pLineEditDefineReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const IComboBoxDefinition* pComboBoxDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pComboBoxDefinition, L"pComboBoxDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const ComboBoxDefinition* pComboBoxDefine = dynamic_cast<const ComboBoxDefinition*>(pComboBoxDefinition))
    {
        return pComboBoxDefine->GetControlDefinitionChangeEvent();
    }
    else if (const ComboBoxDefinitionReference* pComboBoxDefinitionReference = dynamic_cast<const ComboBoxDefinitionReference*>(pComboBoxDefinition))
    {
        return pComboBoxDefinitionReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const IProgressBarDefinition* pProgressBarDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pProgressBarDefinition, L"pProgressBarDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const ProgressBarDefinition* pProgressBarDefine = dynamic_cast<const ProgressBarDefinition*>(pProgressBarDefinition))
    {
        return pProgressBarDefine->GetControlDefinitionChangeEvent();
    }
    else if (const ProgressBarDefinitionReference* pProgressBarDefineReference = dynamic_cast<const ProgressBarDefinitionReference*>(pProgressBarDefinition))
    {
        return pProgressBarDefineReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const ICommandButtonDefinition* pCommandButtonDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCommandButtonDefinition, L"pCommandButtonDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const CommandButtonDefinition* pCommandButtonDefine = dynamic_cast<const CommandButtonDefinition*>(pCommandButtonDefinition))
    {
        return pCommandButtonDefine->GetControlDefinitionChangeEvent();
    }
    else if (const CommandButtonDefinitionReference* pCommandButtonDefineReference = dynamic_cast<const CommandButtonDefinitionReference*>(pCommandButtonDefinition))
    {
        return pCommandButtonDefineReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const IGalleryDefinition* pGalleryDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGalleryDefinition, L"pCommandButtonDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const GalleryDefinition* pGalleryDefine = dynamic_cast<const GalleryDefinition*>(pGalleryDefinition))
    {
        return pGalleryDefine->GetControlDefinitionChangeEvent();
    }
    else if (const GalleryDefinitionReference* pGalleryDefineReference = dynamic_cast<const GalleryDefinitionReference*>(pGalleryDefinition))
    {
        return pGalleryDefineReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const ILabelDefinition* pLabelDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLabelDefinition, L"pCommandButtonDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const LabelDefinition* pLabelDefine = dynamic_cast<const LabelDefinition*>(pLabelDefinition))
    {
        return pLabelDefine->GetControlDefinitionChangeEvent();
    }
    else if (const LabelDefinitionReference* pLabelDefineReference = dynamic_cast<const LabelDefinitionReference*>(pLabelDefinition))
    {
        return pLabelDefineReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const ICheckBoxGroupDefinition* pCheckBoxGroupDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCheckBoxGroupDefinition, L"pCheckBoxGroupDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const CheckBoxGroupDefinition* pDefine = dynamic_cast<const CheckBoxGroupDefinition*>(pCheckBoxGroupDefinition))
    {
        return pDefine->GetControlDefinitionChangeEvent();
    }
    else if (const CheckBoxGroupDefinitionReference* pDefinitionReference = dynamic_cast<const CheckBoxGroupDefinitionReference*>(pCheckBoxGroupDefinition))
    {
        return pDefinitionReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const IRadioButtonGroupDefinition* pRadioButtonGroupDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pRadioButtonGroupDefinition, L"pRadioButtonGroupDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const RadioButtonGroupDefinition* pDefine = dynamic_cast<const RadioButtonGroupDefinition*>(pRadioButtonGroupDefinition))
    {
        return pDefine->GetControlDefinitionChangeEvent();
    }
    else if (const RadioButtonGroupDefinitionReference* pDefinitionReference = dynamic_cast<const RadioButtonGroupDefinitionReference*>(pRadioButtonGroupDefinition))
    {
        return pDefinitionReference->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

gcmp::IControlDefinitionChangeEvent* ControlDefinitionUtil::GetControlDefinitionChangeEvent(const ICommandButtonMenuDefinition* pCommandButtonMenuDefinition)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCommandButtonMenuDefinition, L"pCommandButtonDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const CommandButtonMenuDefinition* pCommandButtonMenuDefine = dynamic_cast<const CommandButtonMenuDefinition*>(pCommandButtonMenuDefinition))
    {
        return pCommandButtonMenuDefine->GetControlDefinitionChangeEvent();
    }
    else if (const CommandButtonMenuDefinitionReference* pCommandButtonMenuDefineRef = dynamic_cast<const CommandButtonMenuDefinitionReference*>(pCommandButtonMenuDefinition))
    {
        return pCommandButtonMenuDefineRef->GetControlDefinitionChangeEvent();
    }
    else
    {
        DBG_WARN(L"类型不正确",L"GDMPLab",L"2024-03-30");
    }
    return nullptr;
}

bool ControlDefinitionUtil::IsReference(const IControlDefinition* pControlDef)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlDef, L"pControlDef为空",L"GDMPLab",L"2024-03-30");
    ControlDefinitionType type = pControlDef->GetType();
    switch (type)
    {
    case gcmp::ControlDefinitionType::CommandButton:
        if (const CommandButtonDefinitionReference* pBtnref = dynamic_cast<const CommandButtonDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::CommandButtonMenu:
        if (const CommandButtonMenuDefinitionReference* pBtnMenuRef = dynamic_cast<const CommandButtonMenuDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::ComboBox:
        if (const ComboBoxDefinitionReference* pComboBoxRef = dynamic_cast<const ComboBoxDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::Gallery:
        if (const GalleryDefinitionReference* pGalleryRef = dynamic_cast<const GalleryDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::LineEdit:
        if (const LineEditDefinitionReference* pLineEditRef = dynamic_cast<const LineEditDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;    
    case gcmp::ControlDefinitionType::Label:
        if (const LabelDefinitionReference* pLabelRef = dynamic_cast<const LabelDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::ProgressBar:
        if (const ProgressBarDefinitionReference* pBarRef = dynamic_cast< const ProgressBarDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::CheckBoxGroup:
        if (const CheckBoxGroupDefinitionReference* pCGroupRef = dynamic_cast<const CheckBoxGroupDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    case gcmp::ControlDefinitionType::RadioButtonGroup:
        if (const RadioButtonGroupDefinitionReference* pRGroupRef = dynamic_cast<const RadioButtonGroupDefinitionReference*>(pControlDef))
        {
            return true;
        }
        break;
    default:
        DBG_WARN(L"不该运行到此处",L"GDMPLab",L"2024-03-30");
        break;
    }
    return false;
}

}